import { Controller, Get, Res } from "@nestjs/common";
import { AppService } from "./app.service";

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get("/test")
  getHello(): string {
    return this.appService.getHello();
  }
  @Get("/gpt-stream")
  async gpt(@Res() res) {
    res.setHeader("Content-Type", "text/plain");
    res.setHeader("Transfer-Encoding", "chunked");
    res.flushHeaders(); //返回头
    const str =
      "你好呀，请问你是绝区零的玩家吗？我们荣幸地向您介绍一款专为绝区零玩家打造的小程序——《旋涡小助手》，在这里，一群志同道合的朋友齐聚一堂，分享攻略心得、交流游戏技巧，共同探索游戏中的每一个角落！";
    const items = str.split("");
    const delayMs = 20;
    for (const item of items) {
      await new Promise((resolve) => setTimeout(resolve, delayMs));
      res.write(item);
    }
    res.end();
  }
}
